package source;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public abstract class DateIO {

	public static MyDate readDate() {
		int year, month, day;
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		while (true) {
			/* Input year */
			while (true) {
				System.out.print("Please input the year of date : ");
				try {
					year = Integer.parseInt(reader.readLine());
					if (0 > year) {
						throw new NumberFormatException();
					}
					break;
				} catch (NumberFormatException e) {
					System.out.println("Error : please input an integer between 0 and 2,147,483,647");
				} catch (IOException e) {
					System.out.println("Error : " + e.getMessage());
				}
			}

			/* Input month */
			while (true) {
				System.out.print("Please input the month of date : ");
				try {
					month = Integer.parseInt(reader.readLine());
					if (!(1 <= month && month <= 12)) {
						throw new NumberFormatException();
					}
					break;
				} catch (NumberFormatException e) {
					System.out.println("Error : please input an integer between 1 and 12");
				} catch (IOException e) {
					System.out.println("Error : " + e.getMessage());
				}
			}

			/* Input day */
			while (true) {
				System.out.print("Please input the day of date : ");
				try {
					day = Integer.parseInt(reader.readLine());
					if (!(1 <= day && day <= 31)) {
						throw new NumberFormatException();
					}
					break;
				} catch (NumberFormatException e) {
					System.out.println("Error : please input an integer between 1 and 31");
				} catch (IOException e) {
					System.out.println("Error : " + e.getMessage());
				}
			}

			/* check the date if valid */
			MyDate date = new MyDate(year, month, day);
			if (date.isValid()) {
				return date; 
			} else {
				System.out.println("Please input again.");
			}
		}
	}

	public static void printDate(MyDate date) {
		System.out.println("The next day of this date is : "+date.getYear()+"/"+date.getMonth()+"/"+date.getDay());
	}

}
